home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
jwpsrc.zip
/
CONVERT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-31
|
56KB
|
1,897 lines
/* Copyright (C) Stephen Chung, 1991-1993. All rights reserved. */
/* This distribution of JWP makes use of the Kana-to-Kanji conversion */
/* dictionary of the Kyoto University "Wnn" project. The original wnn */
/* copyright follows... */
/*
* Copyright Kyoto University Research Institute for Mathematical Sciences
* 1987, 1988, 1989, 1990, 1991
* Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991
* Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that all of the following conditions are satisfied:
*
* 1) The above copyright notices appear in all copies
* 2) Both those copyright notices and this permission notice appear
* in supporting documentation
* 3) The name of "Wnn" isn't changed unless substantial modifications
* are made, or
* 3') Following words followed by the above copyright notices appear
* in all supporting documentation of software based on "Wnn":
*
* "This software is based on the original version of Wnn developed by
* Kyoto University Research Institute for Mathematical Sciences (KURIMS),
* OMRON Corporation and ASTEC Inc."
*
* 4) The names KURIMS, OMRON and ASTEC not be used in advertising or
* publicity pertaining to distribution of the software without
* specific, written prior permission
*
* KURIMS, OMRON and ASTEC make no representations about the suitability
* of this software for any purpose. It is provided "as is" without
* express or implied warranty.
*
* Wnn consortium is one of distributors of the official Wnn source code
* release. Wnn consortium also makes no representations about the
* suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* KURIMS, OMRON, ASTEC AND WNN CONSORTIUM DISCLAIM ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL KURIMS, OMRON, ASTEC OR
* WNN CONSORTIUM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "jwp.h"
#define INDEXLEVEL 3
#define SEPARATION (SYSFONT->width / 2)
#define CACHESIZE (5L * 1024L)
typedef struct {
BYTE key[INDEXLEVEL+1];
LONG int offset;
} INDEX;
typedef struct {
BYTE keylen;
BYTE choicelen;
union {
BYTE string[4];
BYTE far *ptr;
} key;
union {
KANJI string[2];
KANJI far *ptr;
} choice;
} CHOICE;
static OFSTRUCT dctof;
static BYTE far *index = NULL;
static LONG int far *offset = NULL;
static int nr_index = 0;
static int words[BUFSIZE];
static CHOICE far *ConvCache = NULL;
static int ConvCacheSize = 0;
static long int ConvRequests = 0L, ConvHits = 0L, ConvUsage = 0L, ConvChoiceChanges = 0L;
static BOOL sizing = FALSE;
KANJI kanji_list[MAXLINELEN];
typedef struct KanjiListStruct {
KANJI far *kanji;
struct KanjiListStruct far *prev, far *next;
} KANJILIST;
typedef struct UserConvListStruct {
BYTE far *kana;
KANJILIST far *list;
struct UserConvListStruct far *prev, far *next;
} USERCONV;
static USERCONV far *UserConversions = NULL;
static USERCONV far *CurrentConv = NULL;
static KANJILIST far *CurrentKanji = NULL;
static BOOL UserConvChanged = FALSE;
static BOOL AfterHitTest = FALSE;
int bytelen (BYTE far *p)
{
int i;
for (i = 0; *p; i++, p++);
return (i);
}
int bytecmp (BYTE far *p1, BYTE far *p2)
{
for (; *p1 == *p2; p1++, p2++) {
if (*p1 == 0) return (0);
}
return (*p1 - *p2);
}
int bytencmp (BYTE far *p1, BYTE far *p2, int n)
{
int i;
for (i = 0; i < n; i++) {
if (p1[i] != p2[i]) return (p1[i] - p2[i]);
if (p1[i] == 0) return (0);
}
return (0);
}
KANJI far *kanjicpy (KANJI far *p1, KANJI far *p2)
{
while (*p1++ = *p2++);
return (p1);
}
int kanjicmp (KANJI far *p1, KANJI far *p2)
{
for (; *p1 == *p2; p1++, p2++) {
if (*p1 == 0) return (0);
}
return (*p1 - *p2);
}
int kanjincmp (KANJI far *p1, KANJI far *p2, int n)
{
int i;
for (i = 0; i < n; i++) {
if (p1[i] != p2[i]) return (p1[i] - p2[i]);
if (p1[i] == 0) return (0);
}
return (0);
}
int kanjilen (KANJI far *s)
{
int i;
for (i = 0; *s; s++, i++);
return (i);
}
int unitlen (UNIT far *s)
{
int i;
for (i = 0; s->kanji; s++, i++);
return (i);
}
KANJI far *kanjicat (KANJI far *d, KANJI far *s)
{
kanjicpy(d + kanjilen(d), s);
return (d);
}
static int ConvHash (BYTE far *key)
{
int i;
long int sum;
for (i = 0, sum = 0L; key[i]; i++) sum += key[i];
return (sum % ConvCacheSize);
}
static int indexcmp (BYTE far *p1, BYTE far *p2)
{
int i;
for (i = 0; i < INDEXLEVEL; i++) {
if (p1[i] != p2[i]) return (p1[i] - p2[i]);
if (p1[i] == '\0') return (0);
}
return (0);
}
static void ReadUserConversions (char *fname)
{
int fd;
int i, j, nr_conv, nr_list, n;
OFSTRUCT of;
USERCONV far *up;
KANJILIST far *kp;
fd = OpenFile(fname, &of, OF_READ);
if (fd < 0) return;
lseek(fd, 0L, 0);
read(fd, &nr_conv, sizeof(int));
for (i = 0; i < nr_conv; i++) {
if (UserConversions == NULL) {
UserConversions = up = StructAlloc(USERCONV);
up->prev = up->next = NULL;
} else {
up->next = StructAlloc(USERCONV);
up->next->prev = up;
up = up->next;
up->next = NULL;
}
up->list = NULL;
read (fd, &n, sizeof(int));
up->kana = BlockAlloc(n);
_lread(fd, up->kana, n);
read (fd, &nr_list, sizeof(int));
for (j = 0; j < nr_list; j++) {
if (up->list == NULL) {
up->list = kp = StructAlloc(KANJILIST);
kp->prev = kp->next = NULL;
} else {
kp->next = StructAlloc(KANJILIST);
kp->next->prev = kp;
kp = kp->next;
kp->next = NULL;
}
read (fd, &n, sizeof(int));
kp->kanji = BlockAlloc(n * sizeof(KANJI));
_lread(fd, kp->kanji, n * sizeof(KANJI));
}
}
close(fd);
}
void WriteUserConversions (char *fname)
{
int fd, n;
OFSTRUCT of;
USERCONV far *up;
KANJILIST far *kp;
if (!UserConvChanged) return;
fd = OpenFile(fname, &of, OF_CREATE | OF_WRITE);
if (fd < 0) {
ErrorMessage(global.hwnd, "WARNING: Cannot write User Conversion Dictionary '%s'!", fname);
return;
}
lseek(fd, 0L, 0);
/* How many conversions? */
for (up = UserConversions, n = 0; up != NULL; up = up->next, n++);
write(fd, &n, sizeof(int));
for (up = UserConversions; up != NULL; up = up->next) {
n = bytelen(up->kana) + 1;
write(fd, &n, sizeof(int));
_lwrite(fd, up->kana, n);
/* How many kanji's? */
for (kp = up->list, n = 0; kp != NULL; kp = kp->next, n++);
write(fd, &n, sizeof(int));
for (kp = up->list; kp != NULL; kp = kp->next) {
n = kanjilen(kp->kanji) + 1;
write(fd, &n, sizeof(int));
_lwrite(fd, kp->kanji, n * sizeof(KANJI));
}
}
close(fd);
}
int InitConversion (void)
{
int fd;
int bytes, n, blocks;
unsigned int i, j;
BYTE far *cp;
INDEX far *ip;
OFSTRUCT idxof;
kanji_list[0] = 0;
fd = OpenFile (global.userdict, &idxo